using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Configuration;
using Starznet.SharePoint.Utilities;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Settings
{
    public class MembershipDeleteRolePage : LayoutsPageBase
    {
        protected Label lblConfirmDelete;
        protected Literal lblConfirm;
        protected Panel pnlConfirmSuccess;
        protected Panel pnlForm;

        private string _sRole;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            _sRole = Request.QueryString["RoleName"].ToString();
            String sConfirm = this.GetGlobalResourceObject("szadm", "DeleteRole_Confirm").ToString();
            this.lblConfirmDelete.Text = String.Format(sConfirm, _sRole);
        }

        protected void BtnDeleteRole_Click(object sender, EventArgs e)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                string[] sUsers = Roles.GetUsersInRole(_sRole);
                Roles.RemoveUsersFromRole(sUsers, _sRole);

                Log.LogInfo("MembershipDeleteRolePage", "BtnDeleteRole_Click", "All users removed from " + _sRole + " role");

                Roles.DeleteRole(_sRole);

                Log.LogInfo("MembershipDeleteRolePage", "BtnDeleteRole_Click", _sRole + " role deleted");
            });

            this.pnlForm.Visible = false;
            this.pnlConfirmSuccess.Visible = true;
        }
    }
}
